package com.common.convert.impl;

import cn.hutool.core.date.DateUtil;
import com.common.convert.ConvertException;
import com.common.convert.Converter;
import com.common.convert.ConverterConfig;
import com.common.convert.FormatSupport;
import com.common.convert.param.ConvertInput;
import com.common.convert.param.ConvertResult;
import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
import org.jodconverter.core.document.DocumentFormat;
import org.jodconverter.core.office.OfficeException;

import java.io.File;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * @author lxw
 */
public class OfficeConverter implements Converter {

    @Override
    public ConvertResult convert(ConvertInput convertInput, String outType) {
        DocumentFormat fromFormat = getFormat(convertInput.getInputType());
        DocumentFormat toFormat = getFormat(outType);
        String outFile = ConverterConfig.OUT_DIR + File.separator + outType + File.separator + DateUtil.today() + File.separator + UUID.randomUUID()+ File.separator;
        try {
            ConverterConfig.DOCUMENT_CONVERTER.convert(new File(convertInput.getInput())).as(fromFormat).to(new File(outFile)).as(toFormat).execute();
        } catch (OfficeException e) {
            throw new ConvertException(e);
        }
        return ConvertResult.builder()
//                .filePath(outFile)
//                .fileType(outType)
                .build();
    }
    private final Set<FormatSupport> supports = new HashSet<>();

    {
        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.DOC, DefaultDocumentFormatRegistry.PNG));
        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.DOC, DefaultDocumentFormatRegistry.JPEG));
        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.DOC, DefaultDocumentFormatRegistry.DOCX));
        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.DOC, DefaultDocumentFormatRegistry.PDF));
        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.DOC, DefaultDocumentFormatRegistry.TXT));

        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.DOCX, DefaultDocumentFormatRegistry.PNG));
        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.DOCX, DefaultDocumentFormatRegistry.JPEG));
        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.DOCX, DefaultDocumentFormatRegistry.PDF));
        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.DOCX, DefaultDocumentFormatRegistry.TXT));

        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.PDF, DefaultDocumentFormatRegistry.JPEG));
        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.PDF, DefaultDocumentFormatRegistry.PNG));
        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.PDF, DefaultDocumentFormatRegistry.TXT));

        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.XLS, DefaultDocumentFormatRegistry.PNG));
        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.XLS, DefaultDocumentFormatRegistry.PDF));
        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.XLS, DefaultDocumentFormatRegistry.TXT));

        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.XLSX, DefaultDocumentFormatRegistry.PDF));
        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.XLSX, DefaultDocumentFormatRegistry.PNG));
        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.XLSX, DefaultDocumentFormatRegistry.TXT));

        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.CSV, DefaultDocumentFormatRegistry.PDF));
        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.CSV, DefaultDocumentFormatRegistry.PNG));
        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.CSV, DefaultDocumentFormatRegistry.TXT));
        supports.add(new FormatSupport(DefaultDocumentFormatRegistry.PPT, DefaultDocumentFormatRegistry.PNG));
    }

    @Override
    public Set<FormatSupport> supports() {
        return supports;
    }

}
